<template>
    <div style="max-width: 960px;margin: 0 auto;">
        <Header></Header>
        <el-container>
            <el-header>
                <div style="font-size: large"><i class="el-icon-mobile"></i> 找回用户名</div>
            </el-header>
            <el-main>
                <el-form style="border-radius: 4px" :model="ruleForm" :rules="rules" ref="ruleForm"
                         class="demo-ruleForm">

                    <el-row>
                        <el-col :span="5"><div class="grid-content bg-purple">
                            <span style="margin-left: 50px;">统一社会信用代码：</span>
                        </div></el-col>
                        <el-col :span="11"><div class="grid-content bg-purple-light">
                            <el-form-item prop="orgId">
                                <el-input type="text"
                                          placeholder="请输入统一社会信用代码"
                                          v-model="ruleForm.orgId"
                                          @blur.prevent="queryMobileByOrgId()"/>
                            </el-form-item>
                        </div></el-col>
                        <el-col :span="1"><div class="grid-content bg-purple">
                            <span style="color:red;">*</span>
                        </div></el-col>
                    </el-row>

                    <el-row>
                        <el-col :span="5"><div class="grid-content bg-purple">
                            <span style="margin-left: 120px;">手机号：</span>
                        </div></el-col>
                        <el-col :span="11"><div class="grid-content bg-purple-light">
                            <el-form-item prop="mobile">
                                <el-input type="text" :disabled="true" placeholder="手机号码" v-model="ruleForm.mobile" />                                </el-form-item>
                        </div></el-col>
                        <el-col :span="1"><div class="grid-content bg-purple">
                            <span style="color:red;">*</span>
                        </div></el-col>
                    </el-row>

                    <el-row>
                        <el-col :span="5"><div class="grid-content bg-purple">
                            <span style="margin-left: 120px;">验证码：</span>
                        </div></el-col>
                        <el-col :span="9"><div class="grid-content bg-purple-light">
                            <el-form-item style=""  prop="authCode">
                                <el-input type="text" placeholder="请输入验证码" v-model="ruleForm.authCode" style="align-items: right;display: inline-block"/>
                            </el-form-item>
                        </div></el-col>
                        <el-col :span="0.5"><div class="grid-content bg-purple">
                            <span style="color: aliceblue">*</span>
                        </div></el-col>
                        <el-col :span="8" style="text-align: left;">
                            <div class="grid-content bg-purple">
                                <span v-show="!show" class="count" ><span style="color: red">{{count}}</span> 秒后可以重新发送</span>
                                <button type="button" v-show="show" v-on:click="getCode">发送验证码</button>
                            </div>
                        </el-col>
                    </el-row>
                    <el-form-item>
                            <el-button style="margin-top: 20px;width: 100px;margin-right: 100px" type="primary"
                                       v-on:click="onSubmit()">下一步
                            </el-button>
                       </el-form-item>
                </el-form>

            </el-main>
        </el-container>
    </div>
</template>

<script>
    import Header from "../../../components/Header";
    import {getUsername, setEmailCodeBefore, SMSValid} from "../../../api/userMG";

    export default {
        name: "mobile",
        components: {Header},
        data() {
            return {
                show: true,
                count: '',
                timer: null,
                checked: false,
                ruleForm: {
                    mobile: '',
                    authCode: '',
                    orgId: ''
                },
                rules: {
                    orgId:[
                        {required: true, message: '请输入统一社会信用代码', trigger: 'blur'},
                    ],
                    authCode: [
                        {required: true, message: '请输入验证码', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            getCode() {
                const TIME_COUNT = 60;
                if (!this.timer) {
                    this.count = TIME_COUNT;
                    this.show = false;
                    this.timer = setInterval(() => {
                        if (this.count > 0 && this.count <= TIME_COUNT) {
                            this.count--;
                        } else {
                            this.show = true;
                            clearInterval(this.timer);
                            this.timer = null;
                        }
                    }, 1000)
                }
                console.log("======开始生成验证码=====");
                this.$axios.get("/api/v1/user/queryMobileByOrgId?orgId="+this.ruleForm.orgId).then(res=>{
                    if(res.data.code === 200) {
                        setEmailCodeBefore();// 生成验证码
                    }
                    else {
                        this.$message.error("手机号码错误！");
                    }
                });

            },
            queryMobileByOrgId() {
                console.log("======获取手机号=====");
                this.$axios.get("/api/v1/user/queryMobileByOrgId?orgId="+this.ruleForm.orgId).then(res=>{
                    if(res.data.code === 200) {
                        this.ruleForm.mobile = res.data.data;
                    }
                    else {
                        this.ruleForm.mobile = res.data.data;
                        this.$message.error(res.data.msg);
                    }
                });
            },
            onSubmit(){
                const _this = this;
                SMSValid(_this.ruleForm.authCode).then(res=>{
                    if(res.code === 200) {
                        getUsername(this.ruleForm).then(res =>{
                                //后台定义 返回数据 ok == 登录成功
                                if (res.code === 200) {
                                    _this.$router.push({name:"SuccessUserName",params:{username:res.data.username}})
                                } else {
                                    _this.$message.error('失败！');
                                }
                            }
                        );
                    }
                });

            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        const _this = this
                        this.$axios.post('/auth', this.ruleForm).then(res => {

                            console.log(res.data)
                            const jwt = res.headers['authorization']
                            const userInfo = res.data.data

                            // 把数据共享出去
                            _this.$store.commit("SET_TOKEN", jwt)
                            _this.$store.commit("SET_USERINFO", userInfo)

                            // 获取
                            console.log(_this.$store.getters.getUser)

                            _this.$router.push("/blogs")
                        })

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>

    .el-main {
        background-color: #FFFFFF;
        line-height: 45px;
        text-align: center;
        font-size: 14px;
    }


    .el-icon-mobile:before {
        font-size: 40px;
    }

</style>